package com.atguigu.gulimall.cart.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.cart.feign.ProductFeignService;
import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.CartItemVo;
import com.atguigu.gulimall.cart.vo.CartVo;
import com.atguigu.gulimall.cart.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

@Controller
public class CartController {


    @Autowired
    CartService cartService;

    @Autowired
    ProductFeignService productFeignService;


    @GetMapping("/getCheckedCartList")
    @ResponseBody
    public R getCheckedCartList() throws Exception {
        CartVo cart = cartService.getCart();
        List<CartItemVo> collect = cart.getItems()
                .stream()
                .filter(item -> {
                    if (item.getCheck()) {
                        return true;
                    } else {
                        return false;
                    }
                }).map(cartItemVo -> {
                    R skuInfo = productFeignService.getSkuPrice(cartItemVo.getSkuId());
                    if (skuInfo.getCode() == 0) {
                        Double price = (Double) skuInfo.get("price");
                        cartItemVo.setPrice(BigDecimal.valueOf(price));
                    }
                    return cartItemVo;
                })
                .collect(Collectors.toList());
        return R.ok().setData(collect);
    }

    @GetMapping("/cart.html")
    public String cartList(Model model) throws Exception {
        UserInfo userInfo = CartInterceptor.threadLocal.get();
        System.out.println(userInfo);
        CartVo cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.updateNumCart(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }


    /**
     * 删除购物项
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteCartItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }


    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer checked) {
        cartService.updateCheckCart(skuId,checked);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes) throws ExecutionException, InterruptedException {

        CartItemVo cartItemVo = cartService.addCartItem(skuId,num);

//        model.addAttribute("items",cartItemVo);

        attributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
//        return "success";
    }

    @GetMapping("addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model){

        CartItemVo cartItemVo = cartService.getCartRedis(skuId);

        model.addAttribute("items",cartItemVo);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }
}
